package com.techsoft.License;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

import com.techsoft.hardwareInfo.Cpu;
import com.techsoft.hardwareInfo.Ethernet;
import com.techsoft.utils.CalendarUtil;
import com.techsoft.utils.DesUtil;

public class AuthorizationCode {
	// Des加密的默认Key
	private final static String key = "xuguojun!@#$%";
	// 默认的最大日期
	private final static String maxDate = "2999-01-01";
	// 永久有效的标志
	public final static String permanent = "Y";
	// 短暂有效的标志
	public final static String brevity = "N";

	/**
	 * @return 返回本机器的机器码， 由CPU序列号和网卡序列号的部分组合而成
	 */
	public static String generateMachineCode() {
		String s = null;
		try {
			s = Cpu.serial().substring(0, 8);
			s = s + Ethernet.getMACAddress().substring(0, 8);
		} catch (IOException e) {
			s = "AEDE00099";
		}
		return s;
	}

	/**
	 * @param machineCode
	 *            机器码
	 * @param date
	 *            有效的日期
	 * @param permanent
	 *            是否永久的标志
	 * @param key
	 *            加密的Key
	 * @return 授权码
	 * @throws Exception
	 */
	private static String generateLicence(String machineCode, String date, String permanent, String key)
			throws Exception {
		if (date.length() != 10) {
			throw new Exception("有效期必须是一个10位长度的字符串， 如2011-01-01!");
		}

		String s = permanent + date + machineCode;
		s = DesUtil.encrypt(s, key);

		return s;
	}

	/**
	 * @param machineCode
	 *            机器码
	 * @param date
	 *            有效的日期
	 * @param Key
	 *            加密的Key
	 * @return 授权码
	 * @throws Exception
	 */
	public static String generateLicence(String machineCode, String date, String Key) throws Exception {
		return AuthorizationCode.generateLicence(machineCode, date, AuthorizationCode.brevity, Key);
	}

	/**
	 * @param machineCode
	 *            机器码
	 * @param Key
	 *            加密的Key
	 * @return 授权码
	 * @throws Exception
	 */
	public static String generateLicenceByKey(String machineCode, String Key) throws Exception {
		return AuthorizationCode.generateLicence(machineCode, AuthorizationCode.maxDate, AuthorizationCode.permanent,
				Key);
	}

	/**
	 * @param machineCode
	 *            机器码
	 * @param date
	 *            有效的日期
	 * @return 授权码
	 * @throws Exception
	 */
	public static String generateLicenceByDate(String machineCode, String date) throws Exception {
		return AuthorizationCode.generateLicence(machineCode, date, AuthorizationCode.brevity, AuthorizationCode.key);
	}

	/**
	 * @param machineCode
	 *            机器码
	 * @return 授权码
	 * @throws Exception
	 */
	public static String generateLicence(String machineCode) throws Exception {
		return AuthorizationCode.generateLicence(machineCode, AuthorizationCode.maxDate, AuthorizationCode.permanent,
				AuthorizationCode.key);
	}

	/**
	 * @param Licence
	 *            授权码
	 * @return 过期日期
	 * @throws IOException
	 * @throws Exception
	 */
	public static String returnDate(String Licence) throws IOException, Exception {
		return returnDate(Licence, AuthorizationCode.key);
	}

	/**
	 * @param Licence
	 *            授权码
	 * @param key
	 *            加密的Key
	 * @return 过期日期
	 * @throws IOException
	 * @throws Exception
	 */
	public static String returnDate(String Licence, String key) throws IOException, Exception {
		String result = AuthorizationCode.maxDate;

		String s = DesUtil.decrypt(Licence, key);
		String machineCode = s.substring(11, s.length());
		if (!machineCode.equalsIgnoreCase(AuthorizationCode.generateMachineCode())) {
			// 设置一个默认过期日期
			result = "1950-01-01";
		} else {
			String isPermanent = s.substring(0, 1);
			if (!isPermanent.equalsIgnoreCase(AuthorizationCode.permanent)) {
				result = s.substring(1, 11);
			}
		}

		return result;
	}

	/**
	 * @param Licence
	 *            授权码
	 * @return 过期剩余天数
	 * @throws IOException
	 * @throws Exception
	 */
	public static Integer returnDays(String Licence) throws IOException, Exception {
		Integer result = 1000;

		String s = DesUtil.decrypt(Licence, key);
		String machineCode = s.substring(11, s.length());
		if (!machineCode.equalsIgnoreCase(AuthorizationCode.generateMachineCode())) {
			result = 0;
		} else {
			String isPermanent = s.substring(0, 1);
			if (!isPermanent.equalsIgnoreCase(AuthorizationCode.permanent)) {
				String date = s.substring(1, 11);
				DateFormat df = DateFormat.getDateInstance();
				Date now = new Date();
				Date validDate = df.parse(date);

				result = CalendarUtil.daysBetween(now, validDate);

				if (result < 0) {
					result = 0;
				}
			}
		}

		return result;
	}
}
